package com.xlh.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.xlh.Utils.Constant;
import com.xlh.Utils.FUti;
import com.xlh.bean.ExitsTitleBean;
import com.xlh.bean.TitleBean;
import com.xlh.outside.R;
import android.util.Log;

import java.util.List;

public class ExitsAdapter extends BaseAdapter {

	private List<ExitsTitleBean> data;
	private LayoutInflater mInflater;
	private Context context;

	private int gridConums;

		public int getGridConums() {
			return gridConums;
		}

	public void setGridConums(int gridConums) {
		this.gridConums = gridConums;
	}

	public int getParentWidth() {
		return parentWidth;
	}

	public void setParentWidth(int parentWidth) {
		this.parentWidth = parentWidth;
	}

	private int parentWidth;


	public ExitsAdapter(Context context, List<ExitsTitleBean> data) {
		// TODO Auto-generated constructor stub
		this.context = context;
		mInflater = LayoutInflater.from( context );
		if ( data!=null ){
			this.data = data;
		}


	}




	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data == null ? 0 : data.size();
	}
	public ExitsTitleBean getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}



	@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
		ViewHolder holder ;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.title_item, parent, false);
			holder = new ViewHolder();
			holder.tvName = (TextView) convertView.findViewById(R.id.title_text);
			convertView.setTag(holder);
		}
		else {
			holder = (ViewHolder) convertView.getTag();
		}

		ExitsTitleBean titleBean = null;
		if ( position == 0){
			for(ExitsTitleBean t : data){
				if (t.getmAction()!=null && t.getmAction().equals("northwest")){
					 titleBean = data.get(position);
				}
			}

		}else if(position == 1){

			for(ExitsTitleBean t : data){
				if (t.getmAction()!=null && t.getmAction().equals("north")){
					titleBean = data.get(position);
				}
			}


		}else if(position == 2){
			for(ExitsTitleBean t : data){
				if (t.getmAction()!=null && t.getmAction().equals("northeast")){
					titleBean = data.get(position);
				}
			}

		}
		else if(position == 3){
			for(ExitsTitleBean t : data){
				if (t.getmAction()!=null && t.getmAction().equals("west")){
					titleBean = data.get(position);
				}
			}

		}else if(position == 4){
			titleBean = new ExitsTitleBean();
			titleBean.setmText("中间");
			titleBean.setmAction("l");

		}else if(position == 5){
			for(ExitsTitleBean t : data){
				if (t.getmAction()!=null && t.getmAction().equals("east")){
					titleBean = data.get(position);
				}
			}

		}
		else if(position == 6){
			for(ExitsTitleBean t : data){
				if (t.getmAction()!=null && t.getmAction().equals("southwest")){
					titleBean = data.get(position);
				}
			}

		}else if(position == 7){
			for(ExitsTitleBean t : data){
				if (t.getmAction()!=null && t.getmAction().equals("south")){
					titleBean = data.get(position);
				}
			}

		}else if(position == 8){
			for(ExitsTitleBean t : data){
				if (t.getmAction()!=null && t.getmAction().equals("southeast")){
					titleBean = data.get(position);
				}
			}

		}

		/*
		if (titleBean==null){
			holder.tvName.setVisibility(View.GONE);
			return convertView;
		}*/

		int height = 40;
		//得到自己的宽度



		ViewGroup.LayoutParams vLparam = holder.tvName.getLayoutParams();
		//vLparam.width = FUti.dip2px(context,parentWidth/gridConums);
		vLparam.height = FUti.dip2px(context,height);

		//vLparam.width =  FUti.dip2px(context,75);



		String color = titleBean.getColor();



		if(color == null) {
			//holder.tvName.setTextColor(Color.parseColor(Constant.DEFAULT_FONT_COLOR ));
			holder.tvName.setText(titleBean.getmText());
		}
		else {
			holder.tvName.setText(Html.fromHtml("<font color='" + color + "'>" + titleBean.getmText() + "</font>"));
		}


		if (titleBean.getmHtml()!=null && !titleBean.getmHtml().equals("")){
			holder.tvName.setText(  Html.fromHtml(  titleBean.getmHtml()) );
		}






		return convertView;
	}

	private class ViewHolder {
		
		private TextView tvName;
	}
	
	public void addTitle(ExitsTitleBean bean) {
		// TODO Auto-generated method stub
		if (data != null) {
			data.add(bean);
		}
		this.notifyDataSetChanged();
	}

		public void setData(List<ExitsTitleBean> mTitleData) {
			// TODO Auto-generated method stub
			if(mTitleData != null) {
				this.data = mTitleData;
		}
		this.notifyDataSetChanged();
	}

	public void clear() {
		// TODO Auto-generated method stub
		if(data != null) {
			data.clear();
		}
		this.notifyDataSetChanged();
	}

	public void addTitle(List<ExitsTitleBean> ts) {
		// TODO Auto-generated method stub
		
		if(this.data != null) {
			
			for (int i = 0; i < ts.size(); i++) {
				this.data.add(ts.get(i));
			}
		}
		this.notifyDataSetChanged();
	}




	
}
